package basic50.fortieth;

import java.util.Scanner;

public class Basic39 {
    public static void main(String[] args) {
        //题目：编写一个函数，输入n为偶数时，调用函数求1/2+1/4+...+1/n,当输入n为奇数时，调用函数1/1+1/3+...+1/n(利用指针函数)
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int n = input.nextInt();
        if(n%2 == 0){
            even(n);
        }else {
            odd(n);
        }
    }

    private static void odd(int n) {
        double sum =0;
        for (double i = 1; i <= n; i+=2) {
            sum += 1 / i;
        }
        System.out.println(sum);
    }

    private static void even(int n) {
        double sum = 0;
        for (double i = 2; i <= n; i+=2) {
            sum += (1.0 / i ) ;
        }
        System.out.println(sum);
    }
}
